package com.yqfk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqfk.auto.JSONAuthentication;
import com.yqfk.entity.YUser;
import com.yqfk.entity.YVac;
import com.yqfk.mapper.YUserMapper;
import com.yqfk.mapper.YVacMapper;
import com.yqfk.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yqfk
 * @since 2022-03-30
 */
@RestController
@RequestMapping("/y-vac")
public class YVacController extends JSONAuthentication {
    @Autowired
    YVacMapper yVacMapper;
    @Autowired
    YUserMapper yUserMapper;
    @ApiOperation(value = "添加用户接种疫苗信息")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody YVac yVac) throws Exception {
        if (yVacMapper.insert(yVac) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "删除用户接种疫苗信息")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = yVacMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改户接种疫苗信息")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestBody YVac yVac) throws Exception {
        if (yVacMapper.updateById(yVac) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "通过用户id查询疫苗接种信息")
    @PostMapping("/findByUserId")
    public void findByUserId(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("user_id", userId);
        Page<YVac> shUserPage = new Page<>(current, size);
        Page<YVac> shUserPages = yVacMapper.selectPage(shUserPage, wrapper);
        List<YVac> records = shUserPages.getRecords();
        int total = yVacMapper.selectCount(wrapper);
        for (YVac a:records){
            a.setYUser(yUserMapper.selectById(a.getUserId()));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("ym", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }

    }
    @ApiOperation(value = "通过接种的育苗名查询")
    @PostMapping("/findByName")
    public void findByName(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String  name, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("name", name);
        Page<YVac> shUserPage = new Page<>(current, size);
        Page<YVac> shUserPages = yVacMapper.selectPage(shUserPage, wrapper);
        List<YVac> records = shUserPages.getRecords();
        int total = yVacMapper.selectCount(wrapper);
        for (YVac a:records){
            a.setYUser(yUserMapper.selectById(a.getUserId()));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("ym", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }

    }
    @ApiOperation(value = "查询全部用户疫苗接种信息")
    @PostMapping("/findAll")
    public void findAll(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<YVac> shUserPage = new Page<>(current, size);
        Page<YVac> shUserPages = yVacMapper.selectPage(shUserPage, null);
        List<YVac> records = shUserPages.getRecords();
        int total = yVacMapper.selectCount(null);
        for (YVac a:records){
            a.setYUser(yUserMapper.selectById(a.getUserId()));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("ym", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}